/*
 * Class
 * RInterpreter
 */

package pl.abstractvoid.rconnector;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import pl.abstractvoid.datamodel.parameters.ParameterSet;
import pl.abstractvoid.datamodel.parameters.exceptions.NoSuchParameterException;
import rcaller.exception.ParseException;

/**
 * Represents the abstract base class for R connectors which can interpret
 * scripts and produce output.
 * 
 * @author Wojciech Szałapski
 */
public abstract class RInterpreter implements Serializable {

    /**
     * R list of input parameters.
     */
    public static String inputRList = "input";
    
    /**
     * R list of output results.
     */
    public static String outputRList = "output";
    
    /**
     * R list of plots directories.
     */
    public static String plotRList = "myPlots";
    
    /**
     * R helper list of results.
     */
    public static String helperRList = "Rhelper";
    
    /**
     * This flag represents whether the R output was redirected to System.out.
     */
    protected boolean outputRedirected = false;
    
    /**
     * Gets the value of the vector of parameters given by it's name.
     * 
     * @param parameterName Name of the vector of parameters in a table.
     * @return ArrayList of consecutive values in the vector of parameters.
     * @throws NoSuchParameterException 
     */
    public abstract ArrayList<Double> getParameterVectorValue(String parameterName)
                                                throws NoSuchParameterException;
    
    /**
     * Gets the value of the single parameter given by it's name.
     * 
     * @param parameterName Name of the parameter to get.
     * @return Parameter's value.
     * @throws NoSuchParameterException
     * @throws ParseException
     */
    public abstract double getSingleParameterValue(String parameterName)
                                          throws NoSuchParameterException,
                                                 ParseException;
    
    /**
     * Load a whole set of parameters to the R instance.
     * 
     * @param parameterSet Parameters of the distribution to load.
     */
    public abstract void loadParameters(ParameterSet parameterSet);

    /**
     * Runs custom code and returns results in the list of given name.
     * 
     * @param customCode Code to run.
     * @param resultList Name of R list with the results.
     */
    public abstract void runCustomCode(String customCode, String resultList);
    
    /**
     * Runs custom code and returns results using the helper list name.
     * 
     * @param customCode Code to run.
     */
    public abstract void runCustomCode(String customCode);
    
    /**
     * Redirects output from the R interpreter to System.out.
     */
    public abstract void redirectOutput();

    /**
     * Performs operations prior to creating the plot and returns a file in 
     * which it is going to be stored.
     * 
     * @return Abstract path to the file where the plot is going to be stored.
     * @throws IOException
     */
    public abstract File startPlot() throws IOException;

    /**
     * Returns an ImageIcon of the plot generated for given file.
     * 
     * @param plotFile File in which plot had to be stored according to 
     *        startPlot method.
     * @return ImageIcon with the plot.
     */
    public abstract ImageIcon getPlot(File plotFile);

    /**
     * Performs operations needed to finish generating plot.
     */
    public abstract void endPlot();
    
    /**
     * Loads custom versions of R functions that should be replaced or 
     * decorated.
     * <p>
     * For functions that should be disabled there are mock functions which 
     * display appropriate messages. There is also a specific version for the 
     * plot function.
     */
    public abstract void loadCustomFunctions();
    
    /**
     * Cleans everything up when the RInterpreter is no longer needed.
     */
    public abstract void cleanUp();
    
    /**
     * Returns the absolute paths to plots which were generated but not yet 
     * displayed and delete them from the buffer.
     * <p>
     * All the files should be marked with deleteOnExit by this function.
     * 
     * @return Absolute paths to all generated and not displayed plots.
     */
    public abstract String[] getPlotsPaths();
}
